﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;

namespace lcs.Web
{
    /// <summary>
    /// HandlerSname 的摘要说明
    /// </summary>
    public class HandlerSname : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            //context.Response.ContentType = "text/javascript";
            //context.Response.Cache.SetNoStore(); 
            //string keyword = context.Request.QueryString["term"];
            //if (keyword != null)
            //{
            //    keyword = System.Web.HttpUtility.UrlDecode(keyword);
            //    string jsonString = "{\"JSON\":[" + string.Join(",",GetFilteredList(keyword)) + "]}";
            //    context.Response.Write(jsonString); // 返回客户端json格式数据       
            //}
            if (context.Request["key"] != null && context.Request["key"] != "")
            {

                context.Response.Write(GetLikeJobTitle(System.Web.HttpUtility.UrlDecode(context.Request["key"])));
            }
            context.Response.Charset = "gb2312";
            //context.Response.ContentEncoding = System.Text.Encoding.UTF8;
            context.Response.ContentType = "application/json";
            context.Response.End();            
        }
        public String GetLikeJobTitle(String keyword)
        {
            String strJson = String.Empty;            
            lcs.BLL.supplierinfo bll = new BLL.supplierinfo();
            DataSet ds = bll.GetListW(" sname like '%" + keyword + "%'");
                if (ds.Tables[0].Rows.Count > 0)
                {
                    //strJson = Newtonsoft.Json.JsonConvert.SerializeObject(ds.Tables[0]);
                    strJson=Newtonsoft.Json.JsonConvert.SerializeObject(ds.Tables[0]);
                }
            return strJson;
        }
        private string[] GetFilteredList(string keyword)
        {
            lcs.BLL.supplierinfo bll = new BLL.supplierinfo();
            DataSet ds = bll.GetList(" sname like '%" + keyword + "%'");

            List<string> proList = new List<string>();

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                //此处选项一般从数据库中读取，然后循环添加给countrylist对象即可。
                proList.Add("{\"sid\":\""+ds.Tables[0].Rows[i]["sid"].ToString()+"\",\"text\":\""+ds.Tables[0].Rows[i]["sname"].ToString()+"\"}");
            }

            return proList.ToArray();
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}